package com.dd.census.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dd.census.entity.Test;
import com.dd.census.service.TestService;
import com.dd.census.tools.TokenHelper;
import com.dd.census.tools.resopnse.ResultResponse;
import com.dd.census.tools.resopnse.emums.ResultStatus;


/**
 * Created at Tue Apr 13 10:33:02 CST 2021 by  w10g <br>
 * Initialize TestController ... <br>
 * 测试 controller视图处理
 *
 * @author  wanghuanan
 * @since 0.0.1
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/census")
public class TestController {

	private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    private final TestService testService;

    /**
    	 * 不分页查询测试列表
     */
    @GetMapping("test/list")
    public ResultResponse<Object> list(Test test) {
    	try {
	        List<Test> list = testService.selectTestList(test);
	        return ResultResponse.buildSuccess(list);
        } catch (Exception e) {
        	// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
    }

	/**
  	 * 分页
   *
   * @return {@link ResultResponse}
   */
    @PostMapping("test/page")
    public ResultResponse<IPage<Test>> page(@RequestBody Test test) {
    	try {
	        IPage<Test> page = testService.selectTestPages(test);
	        return ResultResponse.buildSuccess(page);
         } catch (Exception e) {
        	// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
    }

    /**
    	 * 获取测试详细信息
     */
    @GetMapping(value = "test/{id}")
    public ResultResponse<Object> getInfo(@PathVariable("id") Long id) {
    	try {
        	return ResultResponse.buildSuccess(testService.selectTestById(id));
     	} catch (Exception e) {
        	// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
    }

    /**
    	 * 新增测试
     */
    @PostMapping(value = "test/create")
    public ResultResponse<Object> add(HttpServletRequest request,@RequestBody Test test) {
    	try {
    		test.setCreateBy(TokenHelper.getUserId(request));
    		test.setCreateTime(new Date());
	        return ResultResponse.buildSuccess(testService.insertTest(test));
        } catch (Exception e) {
        	// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
    }

    /**
     	* 修改测试
     */
    @PostMapping(value = "test/update")
    public ResultResponse<Object> edit(HttpServletRequest request,@RequestBody Test test) {
    	try {
    		test.setUpdateBy(TokenHelper.getUserId(request));
    		test.setUpdateTime(new Date());
	        return ResultResponse.buildSuccess(testService.updateTest(test));
        } catch (Exception e) {
        	// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
    }

    /**
     	* 删除测试
     */
    @PostMapping("test/delete/{ids}")
    public ResultResponse<Object> remove(@PathVariable Long[] ids) {
    	try {
	        int con = testService.deleteTestByIds(ids);
	        return ResultResponse.buildSuccess(con);
     	} catch (Exception e) {
        	// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}

    }
}
